package com.tianqu.tms.www.proxy.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.*;
import com.tianqu.tms.www.proxy.actionform.CarProxySearchInputActionForm;
import com.tianqu.tms.www.proxy.viewhelper.CarProxyInputViewHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * 待办受理查询结果页面Action
 * 
 * @author Administrator
 * 
 */
public class CarProxySearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(CarProxySearchResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarProxySearchInputActionForm inputForm = (CarProxySearchInputActionForm) form;
		String licensePlate = inputForm.getLicensePlate();
		String serial = inputForm.getSerial();
		//
		if (LicensePlateConfigConstant.defLicensePlate.equals(licensePlate)) licensePlate = "";
		if (StringUtil.isNullOrBlank(licensePlate) && StringUtil.isNullOrBlank(serial)) {
			setErrorsToRequest(request, TmsConstants.U0040, null);
			return mapping.getInputForward();
		}else if(!StringUtil.isNullOrBlank(licensePlate) && !StringUtil.isNullOrBlank(serial)){
			setErrorsToRequest(request, TmsConstants.U0040, null);
			return mapping.getInputForward();
		}
		//
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		ArrayList<TransferAcceptedInfo> acceptList = accept.getAllAcceptInfoBySerial(serial, licensePlate);
		if (acceptList.size() == 0) {//没有找到数据
			setErrorsToRequest(request, TmsConstants.U0039, null);
			return mapping.getInputForward();
		}else{
			accept = acceptList.get(0);
			if(StringUtil.isNotBlank(accept.getDb_printTime())){//【U1403】车辆已经待办受理，不能重复受理。
				setErrorsToRequest(request, TmsConstants.U1403, null);
				return mapping.getInputForward();
			}else if(StringUtil.isNotBlank(accept.getZy_printTime()) && accept.getStatus() != 0 && accept.getStatus() != 1 && accept.getStatus() != 2 && accept.getStatus() != 3 ){//【U1404】车辆已经过户受理，不能进行待办受理。
				setErrorsToRequest(request, TmsConstants.U1404, null);
				return mapping.getInputForward();
			}else if(acceptList.size() > 1){
				if (StringUtil.isNullOrBlank(licensePlate) && !StringUtil.isNullOrBlank(serial)) setErrorsToRequest(request, TmsConstants.U1405, null);
				else if (!StringUtil.isNullOrBlank(licensePlate) && StringUtil.isNullOrBlank(serial)) setErrorsToRequest(request, TmsConstants.U1406, null);
				return mapping.getInputForward();
			}else{
				//
				CarProxyInputViewHelper view = new CarProxyInputViewHelper();
				if (StringUtils.isNotBlank(accept.getCarOwner())) {
					if (accept.getCarOwner().length() >= 5) view.setIdentification(accept.getOwnerOrgNo());
					else if (accept.getCarOwner().length() < 5) view.setIdentification(accept.getCarOwnerIdCardNo());
				}
				view = getCarProxyInputViewHelper(accept);
				uc.setAttribute(TmsConstants.VIEWHELP, view);
				return mapping.findForward("marketCar");
			}
		}
	}

	private CarProxyInputViewHelper getCarProxyInputViewHelper(TransferAcceptedInfo accept) throws TmsDaoException {
		Codes code = Codes.getInstance();
		CarProxyInputViewHelper view = new CarProxyInputViewHelper();
		view.setStatus(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS, accept.getStatus()));
		view.setSerial(accept.getSerial());// 流水号
		view.setLicensePlate(accept.getLicensePlate());// 车牌号码
		view.setVin(accept.getVin());// vin码
		view.setBasicCarId(accept.getBasicCarId());
		view.setAgentName(accept.getAgentName());
		view.setAgentOwnerIdCardNo(accept.getAgentOwnerIdCardNo());
		view.setAgentAddress(accept.getAgentAddress());
		if(accept.getRegDate()!=null) view.setRegDate(DateUtil.DateToString11(accept.getRegDate()));
		view.setOwnersGroupPhoto(accept.getOwnersGroupPhoto());
		view.setMaterialImages1(accept.getMaterialImages1());
		view.setMaterialImages2(accept.getMaterialImages2());
		view.setMaterialImages3(accept.getMaterialImages3());
		view.setMaterialImages4(accept.getMaterialImages4());
		view.setMaterialImages5(accept.getMaterialImages5());
		view.setMaterialImages6(accept.getMaterialImages6());
		view.setMaterialImages7(accept.getMaterialImages7());
		view.setMaterialImages8(accept.getMaterialImages8());
		view.setMaterialImages9(accept.getMaterialImages9());
		view.setMaterialImages10(accept.getMaterialImages10());
		view.setAgentPhone(accept.getAgentPhone());
		view.setCarType(accept.getCy_carType());
		if(StringUtil.isNotBlank(accept.getCy_carType())) view.setCarTypeName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE,Integer.parseInt(accept.getCy_carType())));
		view.setCardType(accept.getCy_cardType());
		if(StringUtil.isNotBlank(accept.getCy_cardType())) view.setCardTypeName(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,Integer.parseInt(accept.getCy_cardType())));
		view.setModelCode(accept.getModelCode());
		view.setVendorCode(accept.getVendorCode());
		view.setOwnerAddress(accept.getOwnerAddress());
		view.setCarOwner(accept.getCarOwner());
		view.setOwnerPhone(accept.getOwnerPhone());
		view.setRegisterCardNo(accept.getRegisterCardNo());
		if(accept.getOwnerType() == 1) view.setCarOwnerIdCardNo(accept.getCarOwnerIdCardNo());
		else if(accept.getOwnerType() == 2) view.setCarOwnerIdCardNo(accept.getOwnerOrgNo());
		if (accept.getVin() == null || "".equals(accept.getVin())) view.setVinbsflag("1");
		else view.setVinbsflag("2");
		view.setCountryStyle(accept.getCountryStyle());
		if(StringUtil.isNotBlank(accept.getCountryStyle())) view.setCountryStyleName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY,Integer.parseInt(accept.getCountryStyle())));
		view.setCarUse(accept.getCarUse());
		if(StringUtil.isNotBlank(accept.getCarUse())) view.setCarUseName(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,Integer.parseInt(accept.getCarUse())));
		TransferProcessHistory his = new TransferProcessHistory();
		view.setLcList(his.loadtHistoryBySerial(accept.getSerial()));
		view.setMemberId(accept.getMemberId());
		view.setMemberName(accept.getMemberRealName());
		view.setAttachMemberId(accept.getAttachMemberId());
		view.setShortName(accept.getShortName());
		view.setVendorCode(accept.getVendorCode());
		view.setModelCode(accept.getModelCode());
		view.setOwnerType(accept.getOwnerType()+"");
		view.setCarOwnerIdCardNo(accept.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(accept.getOwnerOrgNo());
		view.setCarOwner(accept.getCarOwner());
		view.setOwnerPhone(accept.getOwnerPhone());
		view.setOwnerAddress(accept.getOwnerAddress());
		view.setAgentOwnerIdCardNo(accept.getAgentOwnerIdCardNo());
		view.setAgentName(accept.getAgentName());
		view.setAgentPhone(accept.getAgentPhone());
		view.setAgentAddress(accept.getAgentAddress());
		return view;
	}

	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private CarProxyInputViewHelper toViewHelper(Car entity, TransferAcceptedInfo accept, CarProxyInputViewHelper view)
			throws TmsDaoException {

		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());
		view.setCarStyleId(entity.getCarStyleId());
		view.setCardType(Integer.toString(entity.getCardType()));
		view.setCarType(Integer.toString(entity.getCarType()));
		view.setCarUse(Integer.toString(entity.getCarUse()));
		view.setColor(Integer.toString(entity.getColor()));
		view.setCompanyContactPerson(entity.getCompanyContactPerson());
		if (entity.getCountryStyle() < 0) {
			view.setCountryStyle("");
		} else {
			view.setCountryStyle(Integer.toString(entity.getCountryStyle()));
		}
		view.setEngine(entity.getEngine());

		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerPhone(entity.getOwnerPhone());
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());

		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
		view.setVendorCode(entity.getVendorCode());
		view.setVin(entity.getVin());

		Member member = Member.getInstance();

		// modify by huangheli 20150917 start
		// view.setMemberName(member.getMemNameByMemId(entity.getMemberId()));
		// view.setMemberId(String.valueOf(entity.getMemberId()));
		if (!StringUtil.isNullOrBlank(accept.getMemberId())) {
			view.setMemberName(member.getMemNameByMemId(Integer.valueOf(accept.getMemberId())));
			view.setMemberId(accept.getMemberId());
		}
		// modify by huangheli 20150917 end

		view.setCarScore(String.valueOf(entity.getCarScore()));

		if (entity.getVendorId() != null && !"".equals(entity.getVendorId())) {
			// 车辆品牌
			Vendor vendor = Vendor.getInstance();
			CarStyle carStyle = CarStyle.getInstance();
			VehicleBrand brand = VehicleBrand.getInstance();
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			String str = entity.getVendorId().substring(0, 1);
			if ("S".equals(str)) {
				view.setFromType(1);
				view.setVendorList(vendor.findAllVendorInfo());
				view.setCarStyleList(carStyle.getAllCarStyleInfo(entity.getBrandId()));
				view.setBrandList(brand.getAllVehicleBrandInfo(entity.getVendorId()));
			} else {
				view.setFromType(2);
				view.setVendorList(commercialVehicle.findAllVendorInfo());
				view.setCarStyleList(commercialVehicle.getAllStyleInfo(entity.getBrandId()));
				view.setBrandList(commercialVehicle.getAllVehicleBrandInfo(entity.getVendorId()));
			}
		}
		return view;

	}

	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private CarProxyInputViewHelper toViewHelper1(TransferPersonalCarInfo entity, TransferAcceptedInfo accept,
			CarProxyInputViewHelper view) throws TmsDaoException {

		if (entity == null)
			return view;
		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());
		view.setCarStyleId(entity.getCarStyleId());
		view.setCardType(Integer.toString(entity.getCardType()));
		view.setCarType(Integer.toString(entity.getCarType()));
		view.setCarUse(Integer.toString(entity.getCarUse()));
		view.setColor(Integer.toString(entity.getColor()));
		view.setCompanyContactPerson(entity.getCompanyContactPerson());
		if (entity.getCountryStyle() < 0) {
			view.setCountryStyle("");
		} else {
			view.setCountryStyle(Integer.toString(entity.getCountryStyle()));
		}
		view.setEngine(entity.getEngine());

		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerPhone(entity.getOwnerPhone());
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());
		// modify by huangheli 20150917 start
		Member member = Member.getInstance();
		if (!StringUtil.isNullOrBlank(accept.getMemberId())) {
			view.setMemberName(member.getMemNameByMemId(Integer.valueOf(accept.getMemberId())));
			view.setMemberId(accept.getMemberId());
		}
		// modify by huangheli 20150917 end
		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
		view.setVendorCode(entity.getVendorCode());
		view.setVin(entity.getVin());

		if (entity.getVendorId() != null && !"".equals(entity.getVendorId())) {
			// 车辆品牌
			Vendor vendor = Vendor.getInstance();
			CarStyle carStyle = CarStyle.getInstance();
			VehicleBrand brand = VehicleBrand.getInstance();
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			String str = entity.getVendorId().substring(0, 1);
			if ("S".equals(str)) {
				view.setFromType(1);
				view.setVendorList(vendor.findAllVendorInfo());
				view.setCarStyleList(carStyle.getAllCarStyleInfo(entity.getBrandId()));
				view.setBrandList(brand.getAllVehicleBrandInfo(entity.getVendorId()));
			} else {
				view.setFromType(2);
				view.setVendorList(commercialVehicle.findAllVendorInfo());
				view.setCarStyleList(commercialVehicle.getAllStyleInfo(entity.getBrandId()));
				view.setBrandList(commercialVehicle.getAllVehicleBrandInfo(entity.getVendorId()));
			}
		}
		return view;
	}

}
